package org.jeecg.modules.qy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhyd.oauth.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.modules.qy.entity.QyCommodityCdInfo;
import org.jeecg.modules.qy.entity.QyCommodityProduct;
import org.jeecg.modules.qy.mapper.QyCommodityCdInfoMapper;
import org.jeecg.modules.qy.service.IQyCommodityCdInfoService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: 产地信息
 * @Author: jeecg-boot
 * @Date:   2021-03-10
 * @Version: V1.0
 */
@Service
public class QyCommodityCdInfoServiceImpl extends ServiceImpl<QyCommodityCdInfoMapper, QyCommodityCdInfo> implements IQyCommodityCdInfoService {

    @Override
    public void updateBachList(List<QyCommodityCdInfo> qyQyCommodityCdInfoList){
        if(CollectionUtils.isNotEmpty(qyQyCommodityCdInfoList)){
            String commodityId = qyQyCommodityCdInfoList.get(0).getCommodityId();
            //查询出数据库中所有的集合
            List<QyCommodityCdInfo> oldList = list(new QueryWrapper<QyCommodityCdInfo>().eq("commodity_id",commodityId));

            HashMap<String,QyCommodityCdInfo> hmap = new HashMap<>();
            List<QyCommodityCdInfo>  tempList = new ArrayList<>();
            for(QyCommodityCdInfo pro : qyQyCommodityCdInfoList){
                if(StringUtils.isNotEmpty(pro.getId())){
                    hmap.put(pro.getId(),pro);
                }else{
                    tempList.add(pro);
                }
            }

            int idx = 1;
            if(CollectionUtils.isNotEmpty(oldList)){
                for(QyCommodityCdInfo proOld:oldList){
                    if(hmap.containsKey(proOld.getId())){
                        //包含做修改
                        QyCommodityCdInfo tmp = hmap.get(proOld.getId());
                        tmp.setSort(idx);
                        updateById(tmp);
                        idx++;
                    }else{
                        removeById(proOld.getId());
                    }
                }
            }


            for(QyCommodityCdInfo cp:tempList){
                cp.setSort(idx);
                save(cp);
                idx++;
            }
        }
    }
}
